/**
 * 
 */
$(function(){
    $(document).keyup(function (e) {
        if(e.which==13){
            $('#selectBtnEnter').click();
        }
    })
});
var path = '../';
var pageListUrl = path + 'appUpdate/list';
var pageUrl = path + 'appUpdate/';

//添加弹窗
var addPanel = null;
// ---------------交互代码begin-----------------
	//本页面业务逻辑
	var dictType={
		checkLogic:function(dataObj){
			var result="";
			var isSubmit=true;
			//TODO 逻辑校验，去掉不必要的:
			return isSubmit;
		}
	};
	
	//绑定列表List---begin----所有的操作都围绕列表展开，新增、更新其他比较独立 
	var dataList=new Vue({
		el:"#appUpdateDetailInfoList",
		data:{
			pageInfo : {},
            apps:[],
			app:"",
            vers:[],
            app:"",
			currentDate:"",
			//自定义条件查询---begin--------
			status:"-1",
			appName:"",
			version:"",
			equipmentCode:"",
            startDate:"",
            overDate:"",
			appVersionId:"",

			//自定义条件查询---end--------
			selectIds:[],//多选
			selectedIdx:-1,//保存操作行的索引
			eventSrc:"",//init:页面初始化；search:点击查询；paging:点击翻页；changePageSize:改变页面显示条数
			selectedPageSize:customUtils.pageSize//修改页面数据显示条数
		},
		mounted: function () {
			//初始化页面数据
            this.$data.currentDate = new  Date().getTime();
			this.getList('init');
            this.$http.post(path+"/appInfo/getApps",null).then(function(response){
                this.$data.apps= response.data;
                console.log(response)
            });
            console.log(this.$data)
		},
		// 在 `methods` 对象中定义方法,如果操作中函数的逻辑比较复杂，必须封装到dictType对象
		methods:{
            cleanAV:function () {

				this.$data.appVersionId = "";
            },
		    selectChkBtn:function(e){//用于多选事件
                this.selectIds.push($(e.target).val());
            },
			//搜索、分页列表
			getList:function(eventSrc){				
				//保存事件源
				this.eventSrc=eventSrc;
				//改变页面显示条数
				if(eventSrc=="changePageSize"){
					if(this.pageInfo.pageSize != this.selectedPageSize){
						this.pageInfo.pageSize=this.selectedPageSize;
						$('#pagination').jqPaginator('destroy');
						eventSrc='init';//重新绘制控件
					}else{
						return;
					}
				}				
				var param = {"page":1,"rows":customUtils.pageSize};		
				//初始化时无当前页数，后台有默认第1页，每页10条
				if(this.pageInfo!=undefined && this.pageInfo.pageNum!=undefined){
					param["page"]=this.pageInfo.pageNum;
					param["rows"]=this.pageInfo.pageSize;
				}
                console.log(this.$data);
				//TODO 搜索条件
                var appId = "";
                if(this.$data.app){
                    appId = this.$data.app.appId;
                }
                param["status"]= this.$data.status;
                param["equipmentCode"]=this.$data.equipmentCode;
                param["startDate"]= this.$data.startDate;
                param["overDate"] = this.$data.overDate;
                param["appVersionId"] = this.$data.appVersionId;
                param["param1"] = appId;
				console.log(param);
				this.$http.post(pageListUrl,param).then(function(response){
						dataList.pageInfo = response.body.pagination;
						customUtils.noData('无数据',dataList.pageInfo.pages);
						//页面初始化、搜索时，才需要执行分页控件
						if(eventSrc=='init' || eventSrc=='search'){
							customUtils.paging('#pagination',dataList.pageInfo.pages,customUtils.showPage,function(num){
								//当前页码pageNum与将要翻页的页码num不相等才执行，否则为同一页
								if(dataList.pageInfo.pageNum!=num){			    	
							    	dataList.pageInfo.pageNum=num;
									dataList.getList('paging');	
								}							    	
						    });
						}
	                },function(response){
	                    console.log(response.body);
	                });					
			},	
			//更新状态
			updateStatus:function(index){
				var record = this.pageInfo.list[index];
				var status = record.status==1?0:1;
				var param = {id:record.id,status:status};
				this.$http.post(pageUrl + 'update',param).then(function(response){                   
					record.status=status;
					customUtils.showTips(response,null,1000);
                },function(response){
                    console.log(response.body);
                });		
			},
            changeTime:function(elem){
                this.$data[elem.split("-")[0]] = $("#"+elem).val();
            },
			//初始化更新，更新对话框
			updateInit:function(index){
				var record = this.pageInfo.list[index];
				this.selectedIdx=index;
				var url = pageUrl + 'id/'+record.id;
				this.$http.get(url).then(function(response){                   
					//回显表单数据
					$.each(response.body, function(key) {
						if(addOrUpdate[key]!=undefined){
							addOrUpdate[key] = response.body[key];
						}									 
					});
                },function(response){
                    console.log(response);
                    customUtils.showRequestTips(response,3000);
                });	
				this.addOrUpdatePanel("update");
			},
			//打开添加对话框
			addOrUpdatePanel:function(flag){
				if(flag=="add"){
					//清除表单数据
					$.each(addOrUpdate.$data, function(key) {
						addOrUpdate[key]="";								 
					});
				}
				addPanel = layer.open({
					type : 1,
					title : "添加分类",
					area : [ '530px', 'auto' ],
					shadeClose : false, // 点击遮罩关闭
					content : $('#addPanel')
				});
			}
		}
	});
	//绑定列表List---end---- 
	
	//新增或更新---------begin-------
	var addOrUpdate = new Vue({
		el:"#addPanel",
		data:{
            status:"",
			appName:"",
			version:"",
			equipmentCode:"",
			updateSuccessDate:"",
			updatePlanDate:"",
			id:"",
			//以下是错误信息
			appNameErr:"",
			versionErr:"",
			equipmentCodeErr:"",
			updateSuccessDateErr:"",
			updatePlanDateErr:"",
		},
		methods:{		

			save:function(){
				//逻辑校验
				if(!dictType.checkLogic(this.$data)){
					return;
				}
				//没有id的时候表示新增，否则为更新
				var actionUrl=this.id=="" ? 'save' :'update';
				this.$http.post(pageUrl + actionUrl,this.$data).then(function(response){                   
                  //显示成功或失败信息
					var isSuccess=customUtils.showTips(response.body,addOrUpdate.data,1000);	
					if(actionUrl=='update'){
						var record = dataList.pageInfo.list[dataList.selectedIdx];
						//更新成功，刷新列表中对应的数据
						$.each(record, function(key) {
							if(addOrUpdate[key]!=undefined){
								record[key]=addOrUpdate[key];
							}									 
						});						
					}else if(isSuccess){
						dataList.getList('init');
					}
					if(isSuccess && addPanel)layer.close(addPanel);
                },function(res){
                    console.log(response.body);
                });				
			},
			cancel:function(){
				if(addPanel)layer.close(addPanel);
			}
		}
	});
	//新增或更新---------end-------
	
	
